package cn.tedu.lucky.draw.user.handler;

import cn.tedu.lucky.draw.user.protocol.po.UserLog;
import cn.tedu.lucky.draw.user.service.IUserLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;

/**
 *
 */
@Component
public class UserLogHandler {

    @Autowired
    private IUserLogService userLogService;

    /**
     *
     * @param  serverRequest   代表一个HTTP请求，包含了请求头、请求参数、请求体等信息
     * @return ServerResponse  代表一个HTTP响应，包含了响应头、响应体、状态码等信息
     */
    public Mono<ServerResponse> findAll(ServerRequest serverRequest){
        //实现查询用户日志的业务逻辑
        Flux<UserLog> userLogFlux = userLogService.findAll();
        /**
         * ServerResponse.ok()方法用于创建HTTP 200 响应对象
         * .body()方法是用于设置响应体内容的方法，它通常与ServerResponse.ok()方法一起使用
         * fromPublisher(userLogFlux, UserLog.class)将这个userLogFlux对象转换为响应主体，
         * Spring WebFlux会自动将其序列化为JSON格式并设置Content-Type响应头
         */
        return ServerResponse.ok().body(fromPublisher(userLogFlux, UserLog.class));
    }
}
